import time

import aircv as ac

from common.tools import ToolsUtil


class FindImg:

    # 读取你传入图片后返回该图片的详细信息
    def img_read(self, img_path):
        return ac.imread(img_path)

    # 对比两个图片返回匹配程度
    def get_confidence(self, full_img_path, part_img_path):
        img_full = self.img_read(full_img_path)
        img_part = self.img_read(part_img_path)

        # 对比两个图片的匹配度
        result = ac.find_template(img_full, img_part)
        print(result)

        # 返回匹配程度
        return result['confidence']

    def cap_full_compare_part(self, driver, full_img_name, part_img_name):
        full_img_name = ToolsUtil.get_project_path() + ToolsUtil.build_path("img", "full_img", full_img_name)
        part_img_name = ToolsUtil.get_project_path() + ToolsUtil.build_path("img", "part_img", part_img_name)
        driver.get_screenshot_as_file(full_img_name)
        time.sleep(3)
        confidence = FindImg().get_confidence(full_img_name, part_img_name)
        return confidence


if __name__ == '__main__':
    full_img = ToolsUtil.get_project_path() + ToolsUtil.build_path("img", "full_img", "dangdang_home_page.png")

    part_img = ToolsUtil.get_project_path() + ToolsUtil.build_path("img", "part_img", "dangdang_home_book_icon.png")

    FindImg().get_confidence(full_img, part_img)
